<template>
  <div>
    <el-button v-permission="'sopItem.edit'" :loading="draftLoading" @click="submit(1)">保存草稿
    </el-button>
    <el-button type="primary" @click.native="submit(2)" :loading="submitLoading" v-permission="'sopItem.edit'">发起流程
    </el-button>
  </div>
</template>

<script>
export default {
  name: 'launch',
  props: {
    draftApi: { type: String, default: () => '' },
    launchApi: { type: String, default: () => '' },
    model: { type: Object, default: () => {} },
    callback: {
      type: Function,
    },
    copyFlag: {
      type: Number,
      default: 2,
    },
  },
  data() {
    return {
      draftLoading: false,
      submitLoading: false,
    }
  },
  mounted() {},
  methods: {
    submit(flag) {
      let url = ''
      let data = new FormData()
      if (this.callback) data = this.callback()
      if (!data) return
      data = this.model ? this.model : data
      if (flag == 1) {
        url = this.draftApi
        this.draftLoading = true
      } else {
        url = this.launchApi
        this.submitLoading = true
      }
      if (this.copyFlag == 1) {
        this.$emit('handCopyMain', flag)
        return
      }
      this.$request(url, data, 'post')
        .then((r) => {
          this.$message.success('操作成功')
          this.$emit('workflowUpdateEmit', r)
        })
        .finally(() => {
          if (flag == 1) this.draftLoading = false
          if (flag == 2) this.submitLoading = false
        })
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
